package com.mmall.interceptor;

import com.mmall.common.IConst;
import com.mmall.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 没有配置到项目中
 * 管理员登录及身份验证
 * 自己做的验证用户是否登录及登录用户是否时管理员,不是则跳转到首页.
 * Created by qezhhnjy on 2017/9/27-21:43.
 */
public class CheckAdmin implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        System.out.println("拦截器验证用户身份!!!");
        User user = (User) request.getSession().getAttribute(IConst.CURRENT_USER);
        if (user == null || user.getRole() != IConst.Role.ADMIN) {
            request.getRequestDispatcher("/index.jsp").forward(request, response);
            return false;
        }
        return true;

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
